<template>
  <div class="logincanter">
      <div class="loginbox">
            <h3>login页</h3>
            <el-form ref="ruleform" :rules="rules" :model="loginform" label-width="0px">
                <el-form-item prop="username">
                 <el-input placeholder="请输入内容" prefix-icon="el-icon-user" v-model="loginform.username"></el-input>
                </el-form-item>

                <el-form-item prop="password">
                    <el-input placeholder="请输入密码" prefix-icon="iconfont icon-3702mima" v-model="loginform.password" autocomplete="off" type="password"></el-input>
                </el-form-item>
                

                <el-form-item>
                    <el-button type="primary" @click="chengelogin('ruleform')">登录</el-button>
                    <el-button type="info" @click="restlogin('ruleform')">重置</el-button>
                </el-form-item>
            </el-form>
      </div>
  </div>
</template>

<script>
export default {
    name:"login",
  components:{},
  data(){
    return {
        loginform:{
            username:"admin",
            password:"123456",
        },
         rules: {
         username: [
				{ required: true, message: '请输入用户名称', trigger: 'blur' },
				{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
			],
			password: [
				{ required: true, message: '请输入密码', trigger: 'blur' },
				{ min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'change' }
			],
         }
    }
  },
  created(){},
  mounted(){},
  watch:{},
  computed:{},
  methods:{
      restlogin(ruleform){
          this.$refs[ruleform].resetFields()
      },
      chengelogin(ruleform){
          this.$refs[ruleform].validate(val=>{
              
              if(!val){
                  return this.$message.error("不符合规范的用户名或密码")
              };
 ;
              let p=new Promise((resolve,reject)=>{
                  this.$axios.post("login",{
                      
                        username:this.loginform.username,
                        password:this.loginform.password,
                     }).then(function(val){
                       resolve(val)
                        
                     })
                })
                p.then(val=>{
                    if(val.meta.status!=200){
                    return this.$message.error("用户名或密码错误")
                    }
                    window.sessionStorage.setItem('token',val.data.token)
                     this.$message.success('登录成功');
                     this.$router.push('/main')
                })
            })  
      }
  },
}
</script>
<style lang="css" scoped>
.logincanter{
    height: 100%;
    background-color: rgb(65, 67, 68);
}
.loginbox{
    width: 600px;
    background-color: white;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    padding: 0 40px;
    box-sizing: border-box;
}
</style>